/*
 * Copyright (C) 2010, JDevelopers (jdevelopers@my-pages.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdevelopers.xarf;

import java.util.List;
import java.util.Vector;

/**
 * Java-class, which is representing a schema of a JSON-definition for a x-arf report.
 * 
 * @author Marcel Hartwig
 * @version 0.1
 * @since JXarfValidator 0.1
 */
public abstract class XarfSchema {
	
	private String description;
	private List<XarfSchemaProperty> xarfProperties = null;
	
	/**
	 * Constructor for creating an instance of a XarfSchema.
	 */
	public XarfSchema() {
	}
	
	/**
	 * Returns the description for the x-arf schema.
	 * 
	 * @return the description.
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * Sets the description for this x-arf schema.
	 * 
	 * @param description the description to set
	 */
	protected void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * Adds a property to the x-arf schema.
	 * 
	 * @param property the property to be added.
	 */
	protected void addxarfProperty(XarfSchemaProperty property) {
		if(property != null) {
			if(xarfProperties == null) {
				xarfProperties = new Vector<XarfSchemaProperty>();
			}
			this.xarfProperties.add(property);
		}
	}

	/**
	 * Returns the properties for this x-arf schema.
	 * 
	 * @return array, containing the properties for the x-arf schema.
	 */
	public XarfSchemaProperty[] getXarfProperties() {
		return xarfProperties.toArray(new XarfSchemaProperty[xarfProperties.size()]);
	}
	
	/**
	 * Returns the x-arf property identified by the name of the property.
	 * 
	 * @param propertyName the name of the property.
	 * @return XarfProperty for the given name.
	 */
	public XarfSchemaProperty getXarfProperty(String propertyName) {
		for(XarfSchemaProperty property : xarfProperties) {
			if(property.getPropertyName().equalsIgnoreCase(propertyName)) {
				return property;
			}
		}
		return null;
	}
	
	/**
	 * Checks, if this schema has properties, or not.
	 * 
	 * @return <code>true</code>, if this schema has properties, otherwise <code>false</code>.
	 */
	public boolean hasXarfProperties() {
		return xarfProperties == null || xarfProperties.isEmpty();
	}

	
	/**
	 * Provides the String representation of the <code>XarfSchema</code>.
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("description: ").append(description).append(" [\r\n");
		for(XarfSchemaProperty property : xarfProperties) {
			sb.append(property).append(",\r\n");
		}
		sb.append("]");
		return sb.toString();
	}
	
	
	
}
